package org.joinme.backend.service;

import org.joinme.backend.mapper.ApplicationRecordMapper;
import org.joinme.backend.mapper.CarPoolingRecordMapper;
import org.joinme.backend.mapper.ParticipateRecordMapper;
import org.joinme.backend.mapper.WechatUserMapper;
import org.joinme.backend.model.CarPoolingRecord;
import org.joinme.backend.model.CarPoolingRecordDetail;
import org.joinme.backend.model.ParticipateRecord;
import org.joinme.backend.model.WechatUser;
import org.joinme.backend.util.ConstUtil;
import org.joinme.backend.util.ErrorCodeMsg;
import org.joinme.backend.util.PrjException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CarPoolingService {
    @Autowired
    CarPoolingRecordMapper carPoolingRecordMapper;

    @Autowired
    ParticipateRecordMapper participateRecordMapper;

    @Autowired
    ParticipateService participateService;

    @Autowired
    WechatUserMapper wechatUserMapper;

    //添加拼单记录
    public int addCarPoolingRecord(CarPoolingRecord record){
        record.setCarPoolingId(null);
        record.setCarPoolingStatus(ConstUtil.CARPOOLING_CREATED);
        record.setCurrentNum(ConstUtil.CARPOOLING_INIT_CURRENT_NUM);

        return carPoolingRecordMapper.insertSelective(record);
    }

    //查询可加的正在进行的拼单
    public List<CarPoolingRecord> getCarPoolingList(int userId, int start, int limit){
        return carPoolingRecordMapper.selectAvailableRecords(userId, start * limit, limit);
    }

    //返回可加拼单的总数量
    public int countCarPoolingList(int userId){
        return carPoolingRecordMapper.countAvailableRecords(userId);
    }

    //查询拼单记录详情
    public CarPoolingRecord getCarPoolingRecord(int carPoolingRecordId){
        return carPoolingRecordMapper.selectByPrimaryKey(carPoolingRecordId);
    }

    //更新拼单信息
    public boolean updateCarPoolingRecord(CarPoolingRecord record){
        return carPoolingRecordMapper.updateByPrimaryKeySelective(record) > 0;
    }

    //查询我的正在进行的拼单
    public List<CarPoolingRecord> getCarPoolingListIng(int userId, int start, int limit){
        return carPoolingRecordMapper.selectIngRecords(userId, start * limit, limit);
    }

    //返回我的正在进行的拼单的总数量
    public int countCarPoolingListIng(int userId){
        return carPoolingRecordMapper.countIngRecords(userId);
    }

    //查询我的历史拼单
    public List<CarPoolingRecord> getCarPoolingListEd(int userId, int start, int limit){
        return carPoolingRecordMapper.selectEdRecords(userId, start * limit, limit);
    }

    //返回我的历史拼单的总数量
    public int countCarPoolingListEd(int userId){
        return carPoolingRecordMapper.countEdRecords(userId);
    }

    //返回拼单的详细信息
    public CarPoolingRecordDetail getCarPoolingRecordDetail(int userId, int carPoolingId){
        CarPoolingRecordDetail detail = new CarPoolingRecordDetail();
        CarPoolingRecord record = carPoolingRecordMapper.selectByPrimaryKey(carPoolingId);
        List<WechatUser> participants = wechatUserMapper.selectParticipantsByCarPoolingId(carPoolingId);
        List<WechatUser> applicants = new ArrayList<>();
        ParticipateRecord participateRecord = participateRecordMapper.selectByPrimaryKey(userId, carPoolingId);
        if(participateRecord != null && participateRecord.getParticipateRole() == ConstUtil.ROLE_PUBLISHER){
            applicants = wechatUserMapper.selectApplicantsByCarPoolingId(carPoolingId);
        }
        if(participateRecord == null){
            detail.setParticipateStatus(-1);
        }else if(participateRecord.getParticipateRole() == ConstUtil.ROLE_PUBLISHER){
            detail.setParticipateStatus(1);
        }else{
            detail.setParticipateStatus(0);
        }
        detail.setRecord(record);
        detail.setParticipants(participants);
        detail.setApplicants(applicants);
        return detail;
    }

    //完成拼单
    public int completeCarPoolingRecord(int userId, int carPoolingId) throws PrjException {
        ParticipateRecord participateRecord = participateRecordMapper.selectByPrimaryKey(userId, carPoolingId);
        if(participateRecord == null || participateRecord.getParticipateRole() != ConstUtil.ROLE_PUBLISHER){
            throw new PrjException(ErrorCodeMsg.CAR_POOLING_RECORD_COMPLETE_ERROR);
        }
        //更新状态为完成
        int result = carPoolingRecordMapper.updateStatus(carPoolingId,ConstUtil.CARPOOLING_COMPLETED);
        participateService.updateAllRelatedStatus(carPoolingId,ConstUtil.PARTICIPATE_FINISHED);;

        return result;
    }

    //查询聊天消息
    public String getCarPoolingRecordMessage(int userId, int carPoolingId) throws PrjException{
        ParticipateRecord participateRecord = participateRecordMapper.selectByPrimaryKey(userId, carPoolingId);
        if(participateRecord == null){
            throw new PrjException(ErrorCodeMsg.MESSAGE_GET_ERROR);
        }
        return carPoolingRecordMapper.selectMessage(carPoolingId);
    }

    //添加聊天消息
    public int addCarPoolingRecordMessage(int userId, int carPoolingId, String message) throws PrjException{
        ParticipateRecord participateRecord = participateRecordMapper.selectByPrimaryKey(userId, carPoolingId);
        if(participateRecord == null){
            throw new PrjException(ErrorCodeMsg.MESSAGE_ADD_ERROR);
        }
        String originMes = carPoolingRecordMapper.selectMessage(carPoolingId);//原有信息
        if(originMes == null)
            originMes = "";
        //构建新的信息
        WechatUser user = wechatUserMapper.selectByPrimaryKey(userId);
        String newMes = user.getNickName() + ": " + message + "\n";

        String mes = originMes + newMes;
        return carPoolingRecordMapper.updateMessage(carPoolingId,mes);
    }
}
